package com.huiyujiaoyu.util;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.huiyujiaoyu.powertime.R;


public class PopupWindowUtil {
    /**
     * @param context
     * @param view
     * @param isOutsideTouch 点击外部是否可diss
     * @return
     */
    public static PopupWindow getPopupWindow(final Context context, View view, boolean isOutsideTouch) {
        PopupWindow popupWindow = new PopupWindow(view);
        popupWindow.setOutsideTouchable(isOutsideTouch);
        if (isOutsideTouch) {
            popupWindow.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.white)));// 没有此句点击外部不会消失
        } else {
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                    DisplayUtil.backgroundAlpha(context, 1f, false);
                }

            });
        }
        popupWindow.setFocusable(true);

        return popupWindow;
    }
    /**
     * 在show  PopupWindow 请先调用 DisplayUtil.backgroundAlpha(context, 0.5f, false);
     * @param context
     * @param view
     * @param
     * @return
     */
    public static PopupWindow getPopupWindow(final Context context, View view) {
        PopupWindow popupWindow = new PopupWindow(view);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.white)));// 没有此句点击外部不会消失
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                DisplayUtil.backgroundAlpha(context, 1f, false);
            }

        });
        popupWindow.setFocusable(true);
        return popupWindow;
    }
    //显示在底部
    public static void showBottom(Context context, PopupWindow pw, View view){
        DisplayUtil.backgroundAlpha(context, 0.5f, false);
        pw.showAtLocation(view, Gravity.BOTTOM,0,0);
    }


    /**
     * 拍照pw
     *
     * @param context
     * @param dw
     * @return
     */
    public static PopupWindow getPopupWindowFromBottom(final Context context,
                                                       final PhotoDoWhat dw, String itemone, String itemtwo) {
        View view = LayoutInflater.from(context).inflate(R.layout.lanling_layout_picfrom, null);
        final PopupWindow popupWindow = new PopupWindow(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(android.R.color.transparent)));// 没有此句点击外部不会消失
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.ActionSheetDialogAnimation);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                DisplayUtil.backgroundAlpha(context, 1f,false);
            }

        });
        TextView tv_one,tv_two;
        tv_one = (TextView)view.findViewById(R.id.tv_paizhao);
        tv_two = (TextView) view.findViewById(R.id.tv_xiangce);
        tv_one.setText(itemone);
        tv_two.setText(itemtwo);
        view.findViewById(R.id.tv_paizhao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dw.doone();
            }
        });
        view.findViewById(R.id.tv_xiangce).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dw.dotwo();
            }
        });
        view.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissPw(popupWindow);
            }
        });
        return popupWindow;
    }



    public static void dissPw(PopupWindow pw) {
        if (pw != null && pw.isShowing()) {
            pw.dismiss();
        }
    }
    public interface ShareDoWhat {
        void doone();

        void dotwo();

        void dothree();


    }
    public interface PhotoDoWhat {
        void doone();

        void dotwo();

    }


}
